package view;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;
/**
 * This class creates a new JWindow that iterates
 * through the sprite image at the location passed in.
 * Image must have 2 rows and 3 columns for the diffrent sections
 * @author cadamson
 *
 */
public class Animate extends JWindow{

	private static final long serialVersionUID = -3130083935793227993L;
	private int iterations;
	private int iterationsCompleted;
	private int x;
	private int y;
	private String location;
	private BufferedImage bigImage;
	private BufferedImage image;
	private JLabel label;
	private Timer t;
	
	
	/**
	 * @param String imageloc - The location of the sprite sheet image file. do not include .png at the end
	 * @param int animationIterations - number of times to animate image
	 * @param int ms between frame changes
	 * @author cadamson
	 *
	 */
	public Animate(String imageloc, int animationIterations, int timerInterval){
		
		// Set the window's bounds, centering the window

		if(!RiskFrame.getAnimateOn()){
			return;
		}
		
		
		iterationsCompleted = 0;
		iterations = animationIterations;
		x = 0;
		y = 0;
		location = imageloc;
		
		
		try { bigImage = ImageIO.read(new File(location+".png"));
		} catch (IOException e) {}
		
		try { image = ImageIO.read(new File(location+"Start.png"));
		} catch (IOException e) {}
	
		int width = image.getWidth();
		int height = image.getHeight();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int sX = (screen.width - width) / 2;
		int sY = (screen.height - height) / 2;
		setBounds(sX, sY, width, height);	
		
		Graphics2D g = image.createGraphics();
		g.drawImage(bigImage, 0,0, null);
		
		label = new JLabel();
		label.setIcon(new ImageIcon(image));
		label.addMouseListener(new MouseClickListener());
		add(label);
		
		t = new Timer(timerInterval, new AnimationActionListener());
		t.start();
		
		setVisible(true);
	}
	
	private class AnimationActionListener implements ActionListener {
		
		private int index;
		
		public AnimationActionListener(){
			index = 0;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(index >= 6){
				iterationsCompleted++;
				index = 0;
			}
			if(iterationsCompleted >= iterations ){
				t.stop();
				System.out.println("END");
				setVisible(false);
				dispose();
				return;
			}
			else if(index >=3){
				x = (index-3)*image.getWidth();
				y = image.getHeight();
			}
			else {
				x = index*image.getWidth();
				y = 0;
			}
						
			index++;
			
			BufferedImage subImage = bigImage.getSubimage(x, y, image.getWidth(), image.getHeight());
			
			Graphics2D g = image.createGraphics();
			g.drawImage(subImage, 0,0, null);
			label.setIcon(new ImageIcon(image));
			validate();
			
		}
	}
	private class MouseClickListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			setVisible(false);
			dispose();
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
//	public static void main(String[] args){
//		new Animate("images/testAnim", 10, 50);
//	}
}

